package com.ruoyi.xiaoshouchuku.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.xiaoshouchuku.domain.saomaChuku;
import com.ruoyi.xiaoshouchuku.domain.saomaChuku2Qianduan;
import com.ruoyi.xiaoshouchuku.domain.saomaTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xiaoshouchuku.mapper.XiaoshouChukuMapper;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import com.ruoyi.common.core.text.Convert;

/**
 * 销售出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-15
 */
@Service
public class XiaoshouChukuServiceImpl implements IXiaoshouChukuService 
{
    @Autowired
    private XiaoshouChukuMapper xiaoshouChukuMapper;

    /**
     * 查询销售出库
     * 
     * @param id 销售出库主键
     * @return 销售出库
     */
    @Override
    public XiaoshouChuku selectXiaoshouChukuById(Long id)
    {
        return xiaoshouChukuMapper.selectXiaoshouChukuById(id);
    }

    /**
     * 查询销售出库列表
     * 
     * @param xiaoshouChuku 销售出库
     * @return 销售出库
     */
    @Override
    public List<XiaoshouChuku> selectXiaoshouChukuList(XiaoshouChuku xiaoshouChuku)
    {
        return xiaoshouChukuMapper.selectXiaoshouChukuList(xiaoshouChuku);
    }

    /**
     * 新增销售出库
     * 
     * @param xiaoshouChuku 销售出库
     * @return 结果
     */
    @Override
    public int insertXiaoshouChuku(XiaoshouChuku xiaoshouChuku)
    {
        return xiaoshouChukuMapper.insertXiaoshouChuku(xiaoshouChuku);
    }

    /**
     * 修改销售出库
     * 
     * @param xiaoshouChuku 销售出库
     * @return 结果
     */
    @Override
    public int updateXiaoshouChuku(XiaoshouChuku xiaoshouChuku)
    {
        return xiaoshouChukuMapper.updateXiaoshouChuku(xiaoshouChuku);
    }

    /**
     * 批量删除销售出库
     * 
     * @param ids 需要删除的销售出库主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouChukuByIds(String ids)
    {
        return xiaoshouChukuMapper.deleteXiaoshouChukuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除销售出库信息
     * 
     * @param id 销售出库主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouChukuById(Long id)
    {
        return xiaoshouChukuMapper.deleteXiaoshouChukuById(id);
    }

    @Override
    public String selectGouhuoDanhaoByLastOne() {
        return xiaoshouChukuMapper.selectGouhuoDanhaoByLastOne();
    }

    @Override
    public List<XiaoshouChuku> selectXiaoshouChukuListNoDo(String userName) {
        XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
        xiaoshouChuku.setFahuoRen(userName);
        xiaoshouChuku.setZhixing("未执行");
        return xiaoshouChukuMapper.selectXiaoshouChukuList(xiaoshouChuku);
    }

    @Override
    public String selectRole2User(String username) {
        String role = xiaoshouChukuMapper.selectRole2User(username);
        if (role == null) {
            return "0";
        }else {
            return role;
        }
    }

    @Override
    public List<saomaChuku> selectSaomaChuKuList(saomaChuku saomaChuku) {
        return xiaoshouChukuMapper.selectSaomaChuKuList(saomaChuku);
    }

    @Override
    public int selectTodo(String goodname, String chukudanhao, String fahuodanhao) {
        return xiaoshouChukuMapper.selectTodo(goodname,chukudanhao,fahuodanhao);
    }

    @Override
    public boolean insertSaomaChuku(saomaChuku saomaChuku) {
        return xiaoshouChukuMapper.insertSaomaChuku(saomaChuku);
    }

    @Override
    public int selectTodo2(String shangpinBianhao, String chukuDanhao, String fahuoDanhao) {
        return xiaoshouChukuMapper.selectTodo2(shangpinBianhao,chukuDanhao,fahuoDanhao);
    }

    @Override
    public List<saomaTest> selectXiaoshouChukuByDanhao(String fahuoDanhao) {
        return xiaoshouChukuMapper.selectXiaoshouChukuByDanhao(fahuoDanhao);
    }

    @Override
    public String selectRole4User(String username) {
        return xiaoshouChukuMapper.selectRole4User(username);
    }

    @Override
    public List<saomaChuku> selectSaomaChuKu4DanGedanhao(String danhao) {
    return xiaoshouChukuMapper.selectSaomaChuKu4DanGedanhao(danhao);
    }

    @Override
    public int updateSaomaChuku(saomaChuku sc) {
        return xiaoshouChukuMapper.updateSaomaChuku(sc);
    }

    @Override
    public saomaChuku selectXiaosaomaTest4danhaoAndshangpin(String fahuodanhao, String shangpinBianhao) {
        List<saomaChuku> saomaTestList = xiaoshouChukuMapper.selectXiaosaomaTest4danhaoAndshangpin(fahuodanhao, shangpinBianhao);

        if (saomaTestList.size() == 0) {
            return null;
        }

        for (saomaChuku s : saomaTestList) {
            if (s.getShangpinShuliang() > 1) {
                return s;
            }
        }
        return saomaTestList.get(0);
    }

    @Override
    public saomaTest selectsaomaChuku2danhaoAndshangpin(String fahuodanhao, Shangpinxinxi shangpinBianhao) {

        XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
        xiaoshouChuku.setFahuoDanhao(fahuodanhao);
        List<XiaoshouChuku> xiaoshouChukuList = xiaoshouChukuMapper.selectXiaoshouChukuList(xiaoshouChuku);

        saomaTest saomaTest = new saomaTest();

        for (XiaoshouChuku x:xiaoshouChukuList) {
            if (x.getGoodNameMen1().equals(shangpinBianhao.getShangpinName())) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getMen1Shuliang());
            }else if (x.getGoodNameMen2().equals(shangpinBianhao.getShangpinName())) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getMen2Shuliang());
            }else if (x.getGoodNameKuang().equals(shangpinBianhao.getShangpinName())) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getKuangShuliang());
            }else if (shangpinBianhao.getShangpinName().equals("防火锁")) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getShuliang1());
            }else if (shangpinBianhao.getShangpinName().equals("闭门器")) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getShuliang2());
            }else if (shangpinBianhao.getShangpinName().equals("拉爆盖")) {
                saomaTest.setName(shangpinBianhao.getShangpinName());
                saomaTest.setShangpinBianhao(shangpinBianhao.getShangpinBianhao());
                saomaTest.setShuliang(x.getShuliang3());
            }
        }
        if (saomaTest.getName() == null) {
            return null;
        }else {
            return saomaTest;
        }
    }
}
